package com.jcvision.mms.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jcvision.mms.dao.AuditOpinionDao;
import com.jcvision.mms.dao.FrameDao;
import com.jcvision.mms.dao.MmsDao;
import com.jcvision.mms.model.AuditOpinion;
import com.jcvision.mms.model.AuditOpinionExample;
import com.jcvision.mms.model.Frame;
import com.jcvision.mms.model.FrameExample;
import com.jcvision.mms.model.Mms;
import com.jcvision.mms.model.MmsExample;
import com.jcvision.mms.util.Constant;
import com.jcvision.mms.util.ErrorMsg;
import com.jcvision.mms.util.PageBean;

@Controller
@RequestMapping("/module/mms")
public class MmsController extends BaseController {

	@RequestMapping("/toAdd.do")
	public String toAdd(){
		return "forward:/module/mms/add.jsp";
	}
	
	@RequestMapping("/add.do")
	public String add(Mms mms, ModelMap map){
		if(mms == null){
			map.put(ERROR_KEY, ErrorMsg.NULL);
			return ERROR_PAGE;
		}else{
			try{
				MmsDao mmsDao = new MmsDao();
				mms.setCreatedTime(new Date());
				mmsDao.insert(mms);
				return "redirect:/module/frame/toAdd.do?mmsId=" + mms.getId();
			}catch(Exception e){
				map.put(ERROR_KEY, ErrorMsg.DAO_EXCEPTION);
				return ERROR_PAGE;
			}
		}
	}
	
	@RequestMapping("/ajaxAdd.do")
	@ResponseBody
	public Map<String, Object> ajaxAdd(Mms mms){
		Map<String, Object> map = new HashMap<>();
		if(mms == null){
			map.put(ERROR_KEY, ErrorMsg.NULL);
			return map;
		}else{
			try{
				MmsDao mmsDao = new MmsDao();
				mms.setCreatedTime(new Date());
				int count = mmsDao.insert(mms);
				if (count > 0) {
					map.put(AJAX_RESULT_KEY, AJAX_RESULT_SUCCESS);
					map.put("id", mms.getId());
				} else
					map.put(AJAX_RESULT_KEY, AJAX_RESULT_FAIL);
			}catch(Exception e){
				map.put(ERROR_KEY, ErrorMsg.DAO_EXCEPTION);
			}
			return map;
		}
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/list.do")
	public String list(Mms mms, Integer pageNumber, ModelMap map){
		try{
			MmsDao mmsDao = new MmsDao();
			MmsExample mmsExample = new MmsExample();
			if(mms != null && mms.getName() != null){
				mms.setName(new String(mms.getName().getBytes("iso-8859-1"), "UTF-8"));
				mmsExample.createCriteria().andNameLike("%" + mms.getName() + "%");
			}
			PageBean pageBean = new PageBean();
			if(pageNumber != null){
				pageBean.setCurrentPage(pageNumber);
			}
			List<Mms> rows = (List<Mms>) mmsDao.select(mmsExample, pageBean);
			map.put("pageBean", pageBean);
			map.put("rows", rows);
			map.put("stateMap", Constant.stateMap);
			return "forward:/module/mms/list.jsp";
		}catch(Exception e){
			e.printStackTrace();
			map.put(ERROR_KEY, ErrorMsg.DAO_EXCEPTION);
			return ERROR_PAGE;
		}
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/detail.do")
	public String detail(Long id, String fromUrl, ModelMap map){
		map.put(FROM_URL, fromUrl);
		try{
			if(id != null){
				MmsDao mmsDao = new MmsDao();
				Mms mms = (Mms) mmsDao.select(id);
				if(mms != null){
					FrameExample frameExample = new FrameExample();
					frameExample.createCriteria().andMmsIdEqualTo(mms.getId());
					FrameDao frameDao = new FrameDao();
					List<Frame> frames = (List<Frame>) frameDao.select(frameExample);
					
					AuditOpinionExample auditOpinionExample = new AuditOpinionExample();
					auditOpinionExample.createCriteria().andMmsIdEqualTo(mms.getId());
					AuditOpinionDao auditOpinionDao = new AuditOpinionDao();
					List<AuditOpinion> auditOpinions = (List<AuditOpinion>) auditOpinionDao.select(auditOpinionExample);
					
					map.put("mms", mms);
					map.put("frames", frames);
					map.put("auditOpinions", auditOpinions);
					map.put("stateMap", Constant.stateMap);
					return "forward:/module/mms/detail.jsp";
				}
			}
			map.put(ERROR_KEY, ErrorMsg.NOT_EXIST_DB);
		}catch(Exception e){
			e.printStackTrace();
			map.put(ERROR_KEY, ErrorMsg.DAO_EXCEPTION);
		}
		return ERROR_PAGE;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/toModify.do")
	public String toModify(Long id, String fromUrl, ModelMap map){
		map.put(FROM_URL, fromUrl);
		try{
			if(id != null){
				MmsDao mmsDao = new MmsDao();
				Mms mms = (Mms) mmsDao.select(id);
				if(mms != null){
					FrameExample frameExample = new FrameExample();
					frameExample.createCriteria().andMmsIdEqualTo(mms.getId());
					FrameDao frameDao = new FrameDao();
					List<Frame> frames = (List<Frame>) frameDao.select(frameExample);
					
					map.put("mms", mms);
					map.put("frames", frames);
					map.put("stateMap", Constant.stateMap);
					return "forward:/module/mms/modify.jsp";
				}
			}
			map.put(ERROR_KEY, ErrorMsg.NOT_EXIST_DB);
		}catch(Exception e){
			e.printStackTrace();
			map.put(ERROR_KEY, ErrorMsg.DAO_EXCEPTION);
		}
		return ERROR_PAGE;
	}
	
	@RequestMapping("/modify.do")
	public String modify(Mms mms, String fromUrl, ModelMap map){
		try{
			if(mms != null && mms.getId() != null){
				MmsDao mmsDao = new MmsDao();
				mms.setState(Constant.STATE_WAIT);
				int count = mmsDao.updateByPrimaryKeySelective(mms);
				if(count > 0){
					map.put("mms", mms);
					return "redirect:" + fromUrl;
				}
			}
			map.put(ERROR_KEY, ErrorMsg.NOT_EXIST_DB);
		}catch(Exception e){
			e.printStackTrace();
			map.put(ERROR_KEY, ErrorMsg.DAO_EXCEPTION);
		}
		return ERROR_PAGE;
	}
	
	@RequestMapping("/delete.do")
	public String delete(Long id, String fromUrl, ModelMap map){
		try{
			if(id != null){
				MmsDao mmsDao = new MmsDao();
				int count = mmsDao.delete(id);
				if(count > 0){
					return "redirect:" + fromUrl;
				}
			}
			map.put(ERROR_KEY, ErrorMsg.NOT_EXIST_DB);
		}catch(Exception e){
			e.printStackTrace();
			map.put(ERROR_KEY, ErrorMsg.DAO_EXCEPTION);
		}
		return ERROR_PAGE;
	}
	
}
